#include "UdpSever.hpp"
#include <memory>

int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        LOG(FATAL, "too few arguments in cmd\n");
        exit(1);
    }
    std::string clientIP = argv[1];
    uint16_t clientPort = std::stoi(argv[2]);   


    int clientfd = socket(AF_INET, SOCK_DGRAM, 0);
    if (clientfd < 0)
    {
        LOG(FATAL, "client socket error\n");
        exit(1);
    }



    
    struct sockaddr_in sever;
    sever.sin_family = AF_INET;
    sever.sin_addr.s_addr = inet_addr(clientIP.c_str());
    sever.sin_port = htons(clientPort);
    while (true)
    {
        std::string line;
        std::cout << "Please Enter# ";
        std::getline(std::cin, line);
        int n = sendto(clientfd, line.c_str(), line.size(), 0, (struct sockaddr *)&sever, sizeof(sever));
        if (n > 0)
        {
            struct sockaddr_in temp;
            socklen_t len = sizeof(temp);
            char inbuffer[1024];

            int m = recvfrom(clientfd, inbuffer, sizeof(inbuffer)-1, 0, (struct sockaddr *)&temp, &len);
            if (m > 0)
            {
                inbuffer[m] = 0;
                std::cout << "sever say:" << inbuffer << std::endl;
            }
            else
            {
                break;
            }
        }
        else
        {
            break;
        }
    }
}